/**********************************************
** @Author: phy
** @Date:   2017-12-12 14:17:37
** @Last Modified by:   phy
***********************************************/

package models

import (
	"net/url"
	//"fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
	"reflect"
)


func Init() {

	timezone := beego.AppConfig.String("db.timezone")
	dbWebUSer :=  beego.AppConfig.String("db.WebUser")
	dbGameLog :=  beego.AppConfig.String("db.GoldLogs")
	if timezone != "" {
		dbWebUSer = dbWebUSer + "&loc=" + url.QueryEscape(timezone)
		dbGameLog = dbGameLog + "&loc=" + url.QueryEscape(timezone)
	}else{
		dbWebUSer = dbWebUSer + "&loc=" + url.QueryEscape("Asia/Shanghai")
		dbGameLog = dbGameLog + "&loc=" + url.QueryEscape("Asia/Shanghai")
	}
	
	orm.RegisterDataBase("default", "mysql", dbWebUSer)
	orm.RegisterDataBase("dbgoldlog", "mysql", dbGameLog)

	orm.RegisterModel(new(ManageAuthUser), new(ManageRoleAuth), new(ManageAdminUser),
	new(UserPurchaseInfo),new(UserOnlineInfo),new(AllUserOnlineCount),new(GameInfo),
	new(UserGiveGift),new(UserCtrlInfo),new(UserInfo),new(SuperVipInfo),new(UserMailInfo),
	new(NoticeList),new(NoticeCtroInfo),
	new(RoomCheatMain),new(RoomCheatSub),new(RoomBloodOperate),new(RoomCtrlInfo),new(RoomBloodChange),
	new(GMAddRecord),new(GMOperationLog),new(GMForbidUserRecord),new(GMChangeCheatRecord),
	new(DayPlathFormInfo))

	if beego.AppConfig.String("runmode") == "dev" {
		orm.Debug = true
	}
	
	beego.SetStaticPath("/upload","upload");
	beego.SetStaticPath("/download","download");
	TimerTasks(10);
	
}

func TableName(name string) string {
	return beego.AppConfig.String("db.prefix") + name;
}

func GetRoomBloodChangeMax() int64{
	tmp,err := beego.AppConfig.Int64("sm.RoomBloodChangeMax");
	if(err != nil){
		return 2000;
	}
	return tmp;
}

func GetUserLvSignName(data interface{}) string {
	strTmp := "0";
	if reflect.TypeOf(data) != nil{
			strTmp = data.(string)
	}else{
		return "error"
	}
	str := "LVSIGN." + strTmp
 	return beego.AppConfig.String(str)	
}
func GetUserPlatformName(data interface{}) string{
	strTmp := "0";
	if reflect.TypeOf(data) != nil{
		strTmp = data.(string)
	}else{
		return "error"
	}
	str := "PLATFORM." + strTmp
 	return beego.AppConfig.String(str)	
}
func GetUserDeviceName(data interface{}) string{
	strTmp := "1";
	if reflect.TypeOf(data) != nil{
		strTmp = data.(string)
	}else{
		return "error"
	}
	str := "DEVICE." + strTmp
 	return beego.AppConfig.String(str)	
}

func GetUserLoginKindName(data interface{}) string{

	if reflect.TypeOf(data) != nil{
		strTmp := data.(string)
		if strTmp == "1"{
			return "离开房间"
		}else if strTmp == "0"{
			return "进入房间"
		}
	}
	return "error"
}

func GetLockStatus(data interface{}) string{

	if reflect.TypeOf(data) != nil{
		strTmp := data.(string)
		if strTmp == "1"{
			return "锁定"
		}else if strTmp == "0"{
			return "解锁"
		}
	}
	return "error"
}


func GetUserGoldChangeConfTableName() string{

 	return beego.AppConfig.String("USCN.tbname")
}
func GetMailStatus(data interface{}) string{

	if reflect.TypeOf(data) != nil{
		strTmp := data.(string)
		if strTmp == "0"{
			return "未发送"
		}else if strTmp == "1"{
			return "已发送"
		}else if strTmp == "2"{
			return "已领取"
		}else if strTmp == "3"{
			return "已删除"
		}
	}
	return "error"
}

	